package com.qiao.glod.ui.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Gravity;
import android.view.Menu;

import com.qiao.glod.R;
import com.qiao.glod.ui.fragment.DrawerFragment;
import com.qiao.glod.ui.fragment.MainFragment;
import com.qiao.glod.util.DoubclickHandler;
import com.qiao.glod.view.item.MenuItem;

public class MainActivity extends QActivity implements DoubclickHandler.DoubleConfirmedListener,DrawerFragment.OnDrawerItemChangedListener{
    private DrawerLayout mDrawerLayout;
    private Fragment mCurrFragment;
    private ActionBarDrawerToggle mActionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Toolbar_NoActionBar);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initViews() {
        super.initViews();
        mDrawerLayout = findView(R.id.drawer_layout);
        mActionBarDrawerToggle = new ActionBarDrawerToggle(
                this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
        mActionBarDrawerToggle.syncState();

        initMainFragment();
    }

    private void initMainFragment(){
        String className = MainFragment.class.getName();
        mCurrFragment = getSupportFragmentManager().findFragmentByTag(className);
        if(mCurrFragment==null){
            mCurrFragment = Fragment.instantiate(this,className);
            getSupportFragmentManager().beginTransaction().add(R.id.container,mCurrFragment,className).commit();
        }
    }

    @Override
    public void onDrawerItemChanged(int index,MenuItem menu) {
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawer(Gravity.LEFT);
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.hide(mCurrFragment);

        if(index == 0){//首页tag为MainFragment.class.name()
            mCurrFragment = getSupportFragmentManager().findFragmentByTag(MainFragment.class.getName());
        }else {
            mCurrFragment = getSupportFragmentManager().findFragmentByTag(menu.getText());
        }

        if(mCurrFragment==null) {
            mCurrFragment = Fragment.instantiate(this, menu.getFragemtClass());
            fragmentTransaction.add(R.id.container, mCurrFragment,menu.getText());
        }else{
            fragmentTransaction.show(mCurrFragment);
        }
        fragmentTransaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        if (mActionBarDrawerToggle != null && mActionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
            case R.id.action_menu_notify:
                break;
            case R.id.action_menu_search:
                break;
            case R.id.action_menu_about_us:
//                Bundle extras = new Bundle();
//                extras.putString(FeedbackFragment.BUNDLE_KEY_CONVERSATION_ID, mFeedbackAgent.getDefaultConversation().getId());
//                readyGo(FeedBackActivity.class, extras);
                break;
            case R.id.action_menu_feedback:
//                Bundle extras = new Bundle();
//                extras.putString(FeedbackFragment.BUNDLE_KEY_CONVERSATION_ID, mFeedbackAgent.getDefaultConversation().getId());
//                readyGo(FeedBackActivity.class, extras);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            DoubclickHandler.getInstance().watch(this);
        }
    }

    @Override
    public void showTips() {
        Snackbar.make(mDrawerLayout, "再按一次退出..", Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void OnDoubleConfirmed() {
        super.onBackPressed();
    }

}
